package com.pro.controller;

import com.pro.pojo.*;
import com.pro.service.JsLoginRecordService;
import com.pro.service.JsUserService;
import com.pro.utils.*;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author MYH - SiYun
 * @date 2022/1/6 16:33
 * 用户控制层
 */
@RequestMapping("/login")
@Controller
public class JsUserController {

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private JsUserService jsUserService;

    @Autowired
    private JsLoginRecordService jsLoginRecordService;

    @Autowired
    private FastDFSClientUtil fastDFSClientUtil;

    @Autowired
    private RedisCatch redisCatch;
    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/6 19:01
     * 登录
     */
    @RequestMapping("/login")
    public String login(HttpSession session){
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取当前时间
        String format = sdf1.format(new Date());
        //框架获取输入的账号和密码
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String name = authentication.getName();
        UserDetails userDetails = userDetailsService.loadUserByUsername(name);
        //调用方法传入账号与密码
        JsUser user = jsUserService.getUser(authentication.getName(), userDetails.getPassword());
        Map<String, Object> ipAndAddressUtils = addressUtils.getIPAndAddressUtils();
        if(user.getEnabled() == 2){
            return "login/loginBanned";
        }
        //登录时判断session的user是否有内容
        if(session.getAttribute("user")==null){
            //登录添加登录日志
            JsLoginRecord loginRecord=new JsLoginRecord();
            loginRecord.setLoginIp((String) ipAndAddressUtils.get("ip"));
            loginRecord.setLoginAddress((String) ipAndAddressUtils.get("addr"));
            loginRecord.setLoginDate(format);
            loginRecord.setUserId(user.getUserId());
            loginRecord.setLoginType(2);
            loginRecord.setLoginState(2);
            jsLoginRecordService.getJsUserJsLoginRecord(loginRecord);
        }
        if(user!=null){
            session.setAttribute("user",user);
        }
        return "login/index";
    }

    /**
     * @Author 祖铭泽
     * @Date 2022/1/20/0020
     * @description 退出账户
     */
    @RequestMapping("/logout")
    public String logout(){
        return "/login/login";
    }

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/12 9:51
     * 获取后台人员列表
     */
    @RequestMapping("/getUserList")
    @ResponseBody
    public JsonData getUserList(JsUser jsUser){
        List<JsUser> userList = jsUserService.getUserList(jsUser);
        return userList!=null?JsonData.buildSuccess(userList):JsonData.buildError("服务器出错了");
    }

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/12 10:51
     * 高级检索后台人员列表
     */
    @RequestMapping("/getUserListRetrieval")
    @ResponseBody
    public JsonData getUserListRetrieval(JsUser jsUser){
        List<JsUser> userList = jsUserService.getUserListRetrieval(jsUser);
        return userList!=null?JsonData.buildSuccess(userList):JsonData.buildError("服务器出错了");
    }

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/12 10:59
     * 修改用户禁用启用状态
     */
    @RequestMapping("/updateJsUserEnabled")
    @ResponseBody
    public JsonData updateJsUserEnabled(Integer userId,Integer enabled){
        boolean updateJsUserEnabled = jsUserService.updateJsUserEnabled(userId,enabled);
        return updateJsUserEnabled?JsonData.buildSuccess(updateJsUserEnabled):JsonData.buildError("服务器出错了");
    }

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/12 11:00
     * 修改用户账号封停启用状态
     */
    @RequestMapping("/updateJsUserStatus")
    @ResponseBody
    public JsonData updateJsUserStatus(Integer userId,Integer status){
        boolean updateJsUserStatus = jsUserService.updateJsUserStatus(userId,status);
        return updateJsUserStatus?JsonData.buildSuccess(updateJsUserStatus):JsonData.buildError("服务器出错了");
    }

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/12 14:39
     * 根据userId修改用户信息
     */
    @RequestMapping("/updateJsUser")
    @ResponseBody
    public JsonData updateJsUser(JsUser jsUser){
        boolean updateJsUser = jsUserService.updateJsUser(jsUser);
        return updateJsUser?JsonData.buildSuccess(updateJsUser):JsonData.buildError("服务器出错了");
    }

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/12 16:42
     * 根据userId查看用户权限
     */
    @RequestMapping("/getJsUserPower")
    @ResponseBody
    public JsonData getJsUserPower(Integer userId){
        List<Integer> jsUserPower = jsUserService.getJsUserPower(userId);
        return jsUserPower!=null?JsonData.buildSuccess(jsUserPower):JsonData.buildError("服务器出错了");
    }

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/12 20:07
     * 根据userId删除用户权限
     */
    @RequestMapping("/deleteJsUserPower")
    @ResponseBody
    public JsonData deleteJsUserPower(Integer userId){
        boolean deleteJsUserPower = jsUserService.deleteJsUserPower(userId);
        return deleteJsUserPower?JsonData.buildSuccess(deleteJsUserPower):JsonData.buildError("服务器出错了");
    }

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/12 20:09
     * 添加用户权限
     */
    @RequestMapping("/addJsUserPower")
    @ResponseBody
    public JsonData addJsUserPower(JsRoleAuthority jsRoleAuthority){
        boolean addJsUserPower = jsUserService.addJsUserPower(jsRoleAuthority);
        return addJsUserPower?JsonData.buildSuccess(addJsUserPower):JsonData.buildError("服务器出错了");
    }

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/13 9:50
     * 查询所有部门
     */
    @RequestMapping("/getJsDept")
    @ResponseBody
    public JsonData getJsDept(){
        List<JsDept> deptList = ifRedisCacheHaveTree("deptTree");
        JsDept jsDept=new JsDept();
        jsDept.setId(0);
        jsDept.setDeptName("所有部门");
        jsDept.setParentId(-1);
        deptList.add(jsDept);
        return deptList != null ? JsonData.buildSuccess(deptList) : JsonData.buildError("获取失败");
    }
    /**
     * @Author 祖铭泽
     * @Date 2022/2/9/0012
     * @description 判断Redis缓存中是否有数据
     */
    private List<JsDept> ifRedisCacheHaveTree(String cacheName){
        List<JsDept> deptTree = redisCatch.getCacheList(cacheName);
        if(deptTree == null || deptTree.isEmpty()){
            deptTree = jsUserService.getJsDept();
            redisCatch.setCacheList(cacheName,deptTree);
        }
        return deptTree;
    }
    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/13 11:46
     * 根据部门id查询user
     */
    @RequestMapping("/getJsUserById")
    @ResponseBody
    public JsonData getJsUserById(Integer id){
        List<JsDept> getJsUserById = jsUserService.getJsUserById(id);
        return getJsUserById!=null?JsonData.buildSuccess(getJsUserById):JsonData.buildError("服务器出错了");
    }

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/13 11:54
     * 根据部门id修改部门名称
     */
    @RequestMapping("/updateJsDeptNameById")
    @ResponseBody
    public JsonData updateJsDeptNameById(Integer id,String deptName){
        boolean updateJsDeptNameById = jsUserService.updateJsDeptNameById(id, deptName);
        boolean deptTree = redisCatch.deleteObject("deptTree");
        return updateJsDeptNameById && deptTree?JsonData.buildSuccess(updateJsDeptNameById):JsonData.buildError("服务器出错了");
    }

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/13 14:34
     * 根据部门id删除部门
     */
    @RequestMapping("/deleteJsDeptById")
    @ResponseBody
    public JsonData deleteJsDeptById(Integer id){
        boolean deleteJsDeptById = jsUserService.deleteJsDeptById(id);
        boolean deptTree = redisCatch.deleteObject("deptTree");
        return deleteJsDeptById&&deptTree?JsonData.buildSuccess(deleteJsDeptById):JsonData.buildError("服务器出错了");
    }

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/14 11:37
     * 查询所有用户账号
     */
    @RequestMapping("/getJsUserAccount")
    @ResponseBody
    public JsonData getJsUserAccount(){
        List<String> jsUserAccount = jsUserService.getJsUserAccount();
        return jsUserAccount!=null?JsonData.buildSuccess(jsUserAccount):JsonData.buildError("服务器出错了");
    }

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/14 14:02
     * 添加后台用户
     */
    @RequestMapping("/addJsUser")
    @ResponseBody
    public JsonData addJsUser(JsUser jsUser){
        jsUser.setPassword(MD5Util.md5(jsUser.getPassword()));
        jsUser.setRegisterDate(DateUtils.tranferDateTimeToString(new Date()));
        boolean addJsUser = jsUserService.addJsUser(jsUser);
        return addJsUser?JsonData.buildSuccess(jsUser):JsonData.buildError("服务器出错了");
    }

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/14 14:40
     * 查询所有部门
     */
    @RequestMapping("/getAllJsDept")
    @ResponseBody
    public JsonData getAllJsDept(){


        List<JsDept> jsDept = jsUserService.getJsDept();

        return jsDept!=null?JsonData.buildSuccess(jsDept):JsonData.buildError("服务器出错了");
    }

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/15 8:49
     * 根据账号查询userid
     */
    @RequestMapping("/getJsUserByAccount")
    @ResponseBody
    public JsonData getJsUserByAccount(String account){
        Integer jsUserByAccount = jsUserService.getJsUserByAccount(account);
        return jsUserByAccount!=null?JsonData.buildSuccess(jsUserByAccount):JsonData.buildError("服务器出错了");
    }

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/15 8:58
     * 添加用户同时添加部门
     */
    @RequestMapping("/addJsUserDept")
    @ResponseBody
    public JsonData addJsUserDept(Integer userId,Integer deptId){
        boolean addJsUserDept = jsUserService.addJsUserDept(userId,deptId);
        return addJsUserDept?JsonData.buildSuccess(addJsUserDept):JsonData.buildError("服务器出错了");
    }

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/15 10:25
     * 查询所有后台解封日志
     */
    @RequestMapping("/getJsUnseal")
    @ResponseBody
    public JsonData getJsUnseal(JsUnseal jsUnseal){
        List<JsUnseal> getJsUnseal = jsUserService.getJsUnseal(jsUnseal);
        return getJsUnseal!=null?JsonData.buildSuccess(getJsUnseal):JsonData.buildError("服务器出错了");
    }

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/15 10:44
     * 根据id删除后台解封日志
     */
    @RequestMapping("/deleteJsUnseal")
    @ResponseBody
    public JsonData deleteJsUnseal(Integer id){
        boolean deleteJsUnseal = jsUserService.deleteJsUnseal(id);
        return deleteJsUnseal?JsonData.buildSuccess(deleteJsUnseal):JsonData.buildError("服务器出错了");
    }

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/15 11:03
     * 查询所有部门
     */
    @RequestMapping("/getJsDeptList")
    @ResponseBody
    public JsonData getJsDeptList(JsDept jsDept){
        List<JsDept> jsDeptList = jsUserService.getJsDeptList(jsDept);
        return jsDeptList!=null?JsonData.buildSuccess(jsDeptList):JsonData.buildError("服务器出错了");
    }

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/15 14:06
     *根据部门id修改部门状态
     */
    @RequestMapping("/updateJsDeptStatus")
    @ResponseBody
    public JsonData updateJsDeptStatus(Integer id, Integer status){
        boolean updateJsDeptStatus = jsUserService.updateJsDeptStatus(id, status);
        return updateJsDeptStatus?JsonData.buildSuccess(updateJsDeptStatus):JsonData.buildError("服务器出错了");
    }

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/15 14:00
     * 根据部门id修改部门信息
     */
    @RequestMapping("/updateJsDept")
    @ResponseBody
    public JsonData updateJsDept(JsDept jsDept){
        boolean updateJsDept = jsUserService.updateJsDept(jsDept);
        return updateJsDept?JsonData.buildSuccess(updateJsDept):JsonData.buildError("服务器出错了");
    }

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/21 15:15
     * 查询所有后台登录日志
     */
    @RequestMapping("/getBackStageLogin")
    @ResponseBody
    public JsonData getBackStageLogin(JsLoginRecord jsLoginRecord){
        List<JsLoginRecord> getBackStageLogin = jsUserService.getBackStageLogin(jsLoginRecord);
        return getBackStageLogin!=null?JsonData.buildSuccess(getBackStageLogin):JsonData.buildError("服务器出错了");
    }

    /**
     *
     * @author MYH - SiYun
     * @date 2022/1/21 15:35
     * 根据id删除登录日志
     */
    @RequestMapping("/deleteLoginRecord")
    @ResponseBody
    public JsonData deleteLoginRecord(Integer id){
        boolean deleteLoginRecord = jsUserService.deleteLoginRecord(id);
        return deleteLoginRecord?JsonData.buildSuccess(deleteLoginRecord):JsonData.buildError("服务器出错了");
    }

    /**
     * @Author 祖铭泽
     * @Date 2022/2/21/0007
     * @description 上传用户头像 图片
     */
    @ResponseBody
    @RequestMapping(value = "/uploadHeadPortrait")
    public JsonData uploadHeadPortrait(MultipartFile headPortrait){
        if(headPortrait.getSize() > 0){
            HashMap<String, Object> map = new HashMap<>();
            String videoUrl = null;
            try {
                videoUrl = fastDFSClientUtil.uploadFile(headPortrait);
            } catch (IOException e) {
                e.printStackTrace();
            }
            String src = fastDFSClientUtil.getResAccessUrl(videoUrl);
            map.put("src",src);
            map.put("code",200);
            return map.get("src") != null ? JsonData.buildSuccess(map) : JsonData.buildError("上传头像失败");
        }
        return null;
    }
    /**
     * @Author 祖铭泽
     * @Date 2022/2/23/0022
     * @description 添加部门
     */
    @ResponseBody
    @RequestMapping(value = "/addDept")
    public JsonData addDept(JsDept dept) {
        return jsUserService.addDept(dept);
    }
}
